<?php 
class kdtrace_jiachengex extends Controller {
	
	function track( $tracknum  , $lang = "en"){
		if($lang=="ch"){
			$langset = "ch";
			$url 	 = "http://www.jcex.com/Tracking-Result2.asp?WLYD_NUM=".$tracknum."&act=detail" ;
		}else{
			$langset = "en";
			$url 	 = "http://www.jcex.com/EN/Tracking-Result2.asp?WLYD_NUM=".$tracknum."&act=detail" ;
		}
		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array(
			'Accept-Language: zh-CN,zh;q=0.8',
			'Referer: http://www.jcex.com/',
			'User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36' 
		));
		$result = curl_exec($ch);
        curl_close($ch);
		$rtndata = array('tracknum'=>$tracknum,'header'=>array(),'err'=>array(),'eventList'=>array());
		if($lang=="ch"){
			$result = mb_convert_encoding($result , 'UTF-8' , 'GBK' );
		}

//		if(strpos($result , "<strong>没有跟踪信息。</strong>")>0 || strpos($result , "><strong>Not found .</strong>")>0 ){
//			$rtndata['err']['errcode'] = '1';
//			return $rtndata;
//		}
		$result = $this->skipSpaces($result);
		$rtndata['err']['errcode'] = '0';
		
		$w1 = strpos($result , 'id="ddd2">' );
		$w2 = strpos($result , 'id="footcss">' ,$w1);
		$rhtm = substr($result , $w1 , $w2 - $w1);
		$rhtm = str_replace("&nbsp;" , " " , $rhtm);
		$pattern = array(
			'ch'=>"/>目 的 国：<\/td> <td>(.+?)<\/td>/i" , 
			'en'=>"/>Destination Country：<\/td> <td>(.+?)<\/td>/i" , 
		);
		if(preg_match($pattern[$langset], $rhtm, $matches) == 1){
			$rtndata['header']['recipientCity']		= $matches[1];
		};

		$pattern = array(
			'ch'=>"/>签 收 人：<\/td> <td>(.+?)<\/td>/i" ,
			'en'=>"/>Signed Income：<\/td> <td>(.+?)<\/td>/i" ,
		);
		if(preg_match($pattern[$langset], $rhtm, $matches) == 1){
			$rtndata['header']['SignedBy']		= $matches[1];
		};
		$pattern = array(
			'ch'=>"/>签收日期：<\/td> <td>(.+?)<\/td>/i" , 
			'en'=>"/>Receipt Date：<\/td> <td>(.+?)<\/td>/i" , 
		);
		if(preg_match($pattern[$langset], $rhtm, $matches) == 1){
			$rtndata['header']['isDelivered']=true;
		};
		
		$w1 = strpos($rhtm , "<table " );
		$w2 = strpos($rhtm , '</table>' ,$w1);
		$bdhtm = substr($rhtm , $w1 , $w2 - $w1);
		$bdhtm = str_replace("&nbsp;" , " " , $bdhtm);
		
		preg_match_all("/<td height\=\"30\">(.+?)<\/td> <td height\=\"30\">(.+?)<\/td> <td height\=\"30\">(.+?)<\/td>/i", $bdhtm, $matches);
		
		$count = count($matches[0]);
		if($count>0){
			for($i=0 ; $i < $count ; $i++){
				//$this->skipSpaces($matches[1][$i])
				$tt =  strtotime( trim($matches[1][$i]) );
				$rtndata['eventList'][$i] = array(
					"date"			=> date('Y-m-d',$tt),
					"time"			=> date('h:i:s',$tt),
					"utime"			=> $tt,
					"status"		=> trim($matches[3][$i]),
					"location"		=> trim($matches[2][$i])
				);
			}
		}



		return $rtndata;
	}
	function skipSpaces($t){
			return  trim(preg_replace('/\s{2,}/', ' ', $t));
	}
	
}

?>